from datetime import datetime

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext_lazy as _

from pinax_reports import model_docs as md


try:
    import xlwt
    has_xlwt = True
except:
    has_xlwt = False
    
FORMAT_CHOICES = [
    ('CSV', 'CSV'),
]
if has_xlwt:
    FORMAT_CHOICES.append(('Excel', 'Excel'))
    
FORMAT_CHOICES = tuple(FORMAT_CHOICES)

class ReportModel(models.Model):
    
    package_identifier = models.CharField(_("package identifier"), help_text=_(md.package_identifier_doc) ,max_length=100, unique=True) 
    model_identifier = models.CharField(_("model identifier"), help_text=_(md.model_identifier_doc) ,max_length=100)     
    
    def __unicode__(self):
        return self.package_identifier + '.models.' + self.model_identifier


class Report(models.Model):
    
    slug = models.SlugField(_('slug'), unique=True)    
    title = models.CharField(_('title'), max_length=50, null=True, blank=True) 
    description = models.TextField(_('description'), blank=True)
    format = models.CharField(_('format'), max_length=10, choices=FORMAT_CHOICES)    
    created = models.DateTimeField(_('created'), default=datetime.now)
    modified = models.DateTimeField(_('modified'), default=datetime.now)
    report_models = models.ManyToManyField(ReportModel, blank=True, null=True, verbose_name=_('report_models'))     
    
    def __unicode__(self):
        return self.title    

    
class ReportHistory(Report)   :
    
    user = models.ForeignKey(User, related_name="created_user", verbose_name=_('user'))


    